package com.pt.iwebshop.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pt.iwebshop.mapper.EbProductMapper;
import com.pt.iwebshop.model.EbProduct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service //业务员证
public class EbProductServiceImpl implements EbProductService {

	@Autowired
	private EbProductMapper ebProductMapper;

	/**
	 *
	 * @param psize     第几页
	 * @param pageSize  每页显示多少条
	 * @return
	 */
	@Override
	public Page<EbProduct> selectList(Integer psize, Integer pageSize) {
		//web---service---maper---数据库
		//第2页 ，每页5条
		Page<EbProduct> page = new Page<>(psize, pageSize);
		//null  ： 根据什么条件分页， null不根据任何条件分页
		Page<EbProduct> productPage = ebProductMapper.selectPage(page, null);

		return productPage;
	}
	@Override
	public EbProduct detail(Integer epId){
		return ebProductMapper.selectById(epId);
	}

	@Override
	public List<EbProduct> selectByCateId(Integer cateId, Integer pSize, Integer pageSize) {
		Page<EbProduct> page=  new Page();
		Page<EbProduct> ebProductList = ebProductMapper.selectPage(page, Wrappers.<EbProduct>lambdaQuery()
				.eq(EbProduct::getEpcChildId, cateId));
		return ebProductList.getRecords();
	}

	@Override
	public int getMaxPage(Integer pageSize) {
		//1.得到最大行数  14/8=1   14.0/8=1.7.....
		int rows = ebProductMapper.selectCount(null);
		//2.得到最大页数  行数/每页的条数  向上取整
		int maxPage =(int)Math.ceil((rows*1.0)/pageSize);
		return maxPage;
	}

	@Override
	public List<EbProduct> selectByCondition(String searchText) {
		//1、根据商品名称进行模糊查询 并返回查询的结果
		return ebProductMapper.selectList(Wrappers.<EbProduct>lambdaQuery().like(EbProduct::getEpName,searchText));
	}
}
















